package lfr.ustc.ftp;

import java.io.File;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class StartServer extends Activity {

	protected Button connect = null;
	protected Button exit = null;
	protected Button help = null;
	protected Button set = null;
	protected Button mail = null;

	private Builder settingDialog = null;

	private TextView txt_pic = null;
	private TextView txt_visitFtp = null;
	private ImageView imv_pic = null;
	private Handler mHandler = new Handler();
	private String[] adValue = { "1", "2", "4", "5"

	, "6", "7", "8" };
	private final Integer[] imageList = {

	R.drawable.ftp, R.drawable.share, R.drawable.cry };
	private final String[] showTxt = { "微型FTP服务器，通过wifi在pc端管理手机文件",
			"只要有wifi即可随时随地将您的文件共享给他人", "你还怕没有带数据线吗，你还嫌在手机上管理文件麻烦吗？", };
	private int index;
	private int indexAd;
	SharedPreferences settings;
	SharedPreferences.Editor editor;

	private void exit() {
		System.exit(0);
	}

	public void onCreate(Bundle savedInstanceState)

	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		connect = (Button) this.findViewById

		(R.id.btnConnect);
		exit = (Button) this.findViewById

		(R.id.btnExit);
		txt_pic = (TextView) this.findViewById(R.id.txt_pic);
		imv_pic = (ImageView) this.findViewById(R.id.imv_pic);
		txt_visitFtp = (TextView) this.findViewById(R.id.txt_visitftp);
		help = (Button) this.findViewById(R.id.help);
		set = (Button) this.findViewById(R.id.set);
		mail = (Button) this.findViewById(R.id.mail);

		Context myContext = CommonSettings.getContext();
		if (myContext == null) {
			myContext = getApplicationContext();
			CommonSettings.setContext(myContext);
		}

		settings = getSharedPreferences("share",

		Context.MODE_WORLD_WRITEABLE);
		editor = settings.edit();
		CommonSettings.setSettings(settings);

		set.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				openDialog();
			}
		});
		mail.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				toSendMail();
			}

		});
		help.setOnClickListener(new

		Button.OnClickListener() {
			public void onClick(View v) {
				AlertDialog.Builder

				aboutDailog = new AlertDialog.Builder(

				StartServer.this);
				aboutDailog.setTitle("使用方法");
				aboutDailog

						.setMessage("1.手机开启wifi无线网络\n2.进入该系统后，单击开始\n3.在电脑上接入同一个wifi网络\n4.打开我的电脑，在地址栏输入ftp地址");

				aboutDailog.show();
			}
		});

		/* 登入按钮 事件设置 */
		connect.setOnClickListener(new

		Button.OnClickListener() {
			public void onClick(View v) {
				WifiManager wifiMgr =

				(WifiManager) getSystemService(Context.WIFI_SERVICE);
				int wifiState =

				wifiMgr.getWifiState();
				if (wifiState ==

				WifiManager.WIFI_STATE_ENABLED) {
					if

					(isStorageCanUse()) {

						if (!

						FtpServer.isRunning()) {

							Toast.makeText(StartServer.this, "FTP服务器已启动",

							1000)

							.show();

							startFtpServer();

							connect.setBackgroundResource(R.drawable.stop);

							connect.setText("停止");

							txt_visitFtp.setText("请访问：ftp://"

							+ FtpServer.getWifiIp() + ":8899");

							txt_visitFtp.setBackgroundColor(R.drawable.red);

							// preference.setSummary("请访问：ftp://" +

							// FtpServer.getWifiIp()

							// + ":" + this.port);
						} else {

							exitFtpServer();

							connect.setBackgroundResource(R.drawable.start1);

							connect.setText("开始");

							Toast.makeText(StartServer.this, "FTP服务器已关闭",

							1000)

							.show();

							txt_visitFtp.setText("");

							txt_visitFtp.setBackgroundResource(0);
						}
					} else {

						Toast.makeText(StartServer.this, "没有sdcard或sdcard不可用",

						1000).show();

					}
				} else {
					Toast.makeText

					(StartServer.this, "没有开启无线网络，无法启动", 1000)

					.show();

				}

			}
		});

		new Thread(new Runnable() {
			public void run() {
				while (true) {
					try {

						Thread.sleep(4000);
						Message

						msg = mHandler.obtainMessage(0);

						mHandler.sendMessage(mHandler.obtainMessage());
					} catch

					(InterruptedException e) {

						e.printStackTrace();
					}
				}

			}
		}).start();
		// 接受消息之后更新imageview视图
		mHandler = new Handler() {
			@Override
			public void handleMessage

			(Message msg) {
				switch (msg.what) {
				case 0:
					if (++index ==

					3) {
						index =

						0;
					}

					imv_pic.setImageResource(imageList[index]);
					txt_pic.setText

					(showTxt[index]);

					imv_pic.invalidate();

					break;
				}
			}
		};

		exit.setOnClickListener(new

		Button.OnClickListener() {
			public void onClick(View v) {
				AlertDialog.Builder

				aboutDailog = new AlertDialog.Builder(

				StartServer.this);
				aboutDailog.setTitle("退出");
				aboutDailog

				.setMessage("确定退出吗？");
				aboutDailog.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								exit();
							}
						});
				aboutDailog.setNeutralButton("取消",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								dialog.dismiss();
							}
						});

				aboutDailog.show();
			}
		});

	}

	private void openDialog() {
		Intent intent = new Intent();
		intent.setClass(this, Setting.class);
		startActivity(intent);
	}

	private void toSendMail() {
		Intent intent = new Intent();
		intent.setClass(this, MailManger.class);
		startActivity(intent);
		this.finish();

	}

	/**
	 * 开始ftp服务器
	 */
	public void startFtpServer() {
		Context context = getApplicationContext

		();
		Intent intent = new Intent(context,

		FtpServer.class);
		if (!FtpServer.isRunning()) {
			context.startService(intent);
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		return super.onCreateDialog(id);
	}
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			AlertDialog.Builder

			aboutDailog = new AlertDialog.Builder(

			StartServer.this);
			aboutDailog.setTitle("退出");
			aboutDailog

			.setMessage("退出时是否关闭服务器？");
			aboutDailog.setPositiveButton("是",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							exit();
						}
					});
			aboutDailog.setNeutralButton("后台运行",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							finishOfMain();
						}
					});

			aboutDailog.show();
		} else {
			return super.onKeyDown(keyCode, event);
		}
		return false;
	}
	private void finishOfMain(){
		this.finish();
	}

	private void returnTo(String erroerType) {
		Intent intent = new Intent();
		intent.putExtra("erroerType",

		erroerType);
		intent.setClass(this,

		DialogActivity.class);
		startActivity(intent);
	}

	private boolean isStorageCanUse() {
		String storageState =

		Environment.getExternalStorageState();
		if (!storageState.equals

		(Environment.MEDIA_MOUNTED)) {

			return false;
		}
		return true;
	}

	/**
	 * 关闭ftp服务器
	 */
	public void exitFtpServer() {
		Context context = getApplicationContext

		();
		Intent intent = new Intent(context,

		FtpServer.class);
		if (FtpServer.isRunning()) {
			context.stopService(intent);
		}
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onResume() {
		if (FtpServer.isRunning()) {

			txt_visitFtp.setText("请访问：ftp://"

			+ FtpServer.getWifiIp() + ":8899");
			connect.setBackgroundResource(R.drawable.stop);

			connect.setText("停止");
			txt_visitFtp.setBackgroundColor(R.drawable.red);
		} else {

		}
		super.onResume();
	}

}
